package com.xing.ui.expandableRecyclerView

import android.graphics.Color
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.constraintlayout.motion.widget.MotionScene
import androidx.constraintlayout.motion.widget.TransitionBuilder
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.recyclerview.widget.RecyclerView
import java.util.*

class ExpandableGroupVH(val item: View) : RecyclerView.ViewHolder(
    MotionLayout(item.context).apply {
        //真正的Group容器
        layoutParams = RecyclerView.LayoutParams(
            RecyclerView.LayoutParams.MATCH_PARENT,
            RecyclerView.LayoutParams.MATCH_PARENT
        )
    }) {

    companion object {
        val GROUP_ID = View.generateViewId()
        val CHILD_ROOT_ID = View.generateViewId()
    }

    val childIds: MutableList<Int> by lazy { LinkedList<Int>() }

    val childRoot: LinearLayout by lazy {
        LinearLayout(itemView.context).apply {
            //子Item的容器
            orientation = LinearLayout.VERTICAL
            layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                333
            ).apply {
                topToBottom = GROUP_ID
                leftToLeft = ConstraintSet.PARENT_ID
                rightToRight = ConstraintSet.PARENT_ID
            }

            id = CHILD_ROOT_ID
        }
    }

    init {
        item.apply {
            //Group内容
            id = GROUP_ID
            layoutParams = ConstraintLayout.LayoutParams(
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                topToTop = ConstraintSet.PARENT_ID
                leftToLeft = ConstraintSet.PARENT_ID
                rightToRight = ConstraintSet.PARENT_ID
            }

            (itemView as MotionLayout).addView(this)
        }
        setIsRecyclable(false)
    }

    fun bind(data: ExpandableData) {

        if (data.child != null && data.isExpanded) {
            //需要加载子Item
            data.child?.forEachIndexed { index, expandableData ->
                val id = if (childIds.size > index) {
                    childIds[index]
                } else {
                    childIds.add(View.generateViewId())
                    childIds[index]
                }
                var childView = childRoot.findViewById<TextView>(id)
                if (childView == null) {
                    //此处的缓存手段，不能满足多种不同类型的childView
                    childView = createChildView() as TextView
                    childView.id = childIds[index]

                    childRoot.addView(childView)
                }
                childView.text = expandableData.text
            }
            (itemView as MotionLayout).addView(childRoot)
        }

        (item as TextView).apply {
            text = data.text
            textSize = 50f
            setTextColor(Color.BLACK)
        }

        item.setOnClickListener {

            val motionLayout = itemView as MotionLayout

            val startScene = ConstraintSet().apply {
                clone(motionLayout)
                val height = if (data.isExpanded) childRoot.height else 0
                Log.e("sdfa", "start height = $height")
                constrainHeight(CHILD_ROOT_ID, height)
//                constrainWidth(CHILD_ROOT_ID, height)

//                constrainedHeight(CHILD_ROOT_ID, true)
            }
            val endScene = ConstraintSet().apply {
                clone(motionLayout)
                val height = if (!data.isExpanded) childRoot.height else 0
                Log.e("sdfa", "end height = $height")
                constrainHeight(CHILD_ROOT_ID, height)
//                constrainWidth(CHILD_ROOT_ID, height)
//                constrainedHeight(CHILD_ROOT_ID, true)
            }
            val scene = MotionScene(motionLayout)
            val startID = View.generateViewId()
            val endID = View.generateViewId()
            TransitionBuilder.buildTransition(scene, View.generateViewId(), startID, startScene, endID, endScene).let {
                scene.addTransition(it)
                scene.setTransition(it)
                motionLayout.setScene(scene)
                motionLayout.setTransition(startID, endID)
                motionLayout.transitionToEnd()
            }

            Log.e("sdfa", "修改前 ${data.isExpanded}")
            data.isExpanded = !data.isExpanded
            Log.e("sdfa", "修改后 ${data.isExpanded}")
        }
    }

    open fun createChildView(): View {
        return TextView(itemView.context).apply {
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )

            textSize = 30f
            setTextColor(Color.BLACK)
        }
    }

}